// miniprogram/pages/business/fb/fb.js
const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    currentTab: 0,
    type:["基本信息","商品数量",'活动打折'],
    //分类
    array: ['--请选择--','男:衣服', '男:裤子', '男:鞋子', '男:其他', '女:衣服', '女:裤子', '女:鞋子', '女:其他'],
    express:['--请选择--',0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
    index: 0,
    index2:0,

    imgbox: [],
    imgbox2:[],
    size: [],
    color: [],

    title: "",
    address:'',
    rmb:'',
    sale:'',
    
    fileIDs:[],
    fileIDs2:[],

  
    numbers: [],

  },
 
  //数量处理
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  NumbersInput: function (e) {
    let value = e.detail.value.replace(/\D/g, '')
    var number = this.data.numbers
    var a = parseInt(value)
    console.log(number)
    number[e.target.id] = 0
    number[e.target.id] = a
    console.log(number)
    this.setData({
      //parseInt将数字字符串转换成数字
      numbers: number,
    })
  },

  CutNumber: function (e) {
    console.log(e.target.id)
    var a = parseInt(e.target.id)
    var number = this.data.numbers
    if (number[a] <= 0) {
      number[a] = 0
    } else {
      number[a] = number[a] - 1
    }
    this.setData({
      numbers: number
    })

  },
  AddNumber: function (e) {
    console.log(e.target.id)
    var a = parseInt(e.target.id)
    var number = this.data.numbers
    number[a] = number[a] + 1
    this.setData({
      numbers: number
    })
  },
  
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换 
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  sale:function(e){
    let value = e.detail.value.replace(/\D/g, '')
    var sale = parseInt(value)
    if (value==""){
      sale=0
    }
    console.log(sale )
    this.setData({
      //parseInt将数字字符串转换成数字
      sale: sale
    })
  },

  rmb:function(e){
    let value = e.detail.value.replace(/\D/g, '')
    var rmb = parseInt(value)
    if (value==""){
      rmb=0
    }
    console.log(rmb )
    this.setData({
      //parseInt将数字字符串转换成数字
      rmb: rmb
    })
  },
 
  GetAddress: function (e) {
    console.log(e.detail.value)
    this.setData({
      address: e.detail.value
    })
  },
  GetTitle: function (e) {
    console.log(e.detail.value)
    this.setData({
      title: e.detail.value
    })
  },
  
  //分类选择
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  //快递选择
  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },

  AddColor:function(){
    var color=this.data.color
    color.push("")
    this.setData({
      color: color
    })
  },
  DelectColor:function(e){
    console.log(e.currentTarget.dataset.id)
    var color=this.data.color
    color.splice(e.currentTarget.dataset.id,1);                                                                                 console.log(color)
    this.setData({
      color: color
    })                               
  },
  AddSize:function(){
    var size=this.data.size
    size.push("")
    this.setData({
      size: size
    })
  },
  DelectSize:function(e){
    console.log(e.currentTarget.dataset.id)
    var size=this.data.size
    size.splice(e.currentTarget.dataset.id,1);                                                                                 console.log(size)
    this.setData({
      size: size
    })                               
  },
  // 尺码
  InputSize:function(e){
    var that=this;
    console.log(e)
    console.log(e.detail.value)
    console.log(e.target.dataset.id)
    var index = e.target.dataset.id
    var value = e.detail.value
    var size = that.data.size
    size[index] = value
    console.log(size)
    that.setData({
      size :size
    })
  },

  // 颜色
  InputColor:function(e){
    var that=this;
    console.log(e)
    console.log(e.detail.value)
    console.log(e.target.dataset.id)
    var index = e.target.dataset.id
    var value = e.detail.value
    var color = that.data.color
    color[index] = value
    console.log(color)
    that.setData({
      color:color
    })
  },
  ModifyInformation:function(){
    var that=this
    wx.showLoading({
      title: '修改中',
    })
    db.collection('Goods').doc(that.data.id).update({
      data: {
        title: that.data.title,
        type: that.data.array[that.data.index],
        rmb: that.data.rmb,
        size: that.data.size,
        color: that.data.color,
        numbers: that.data.numbers,
        express:that.data.express[that.data.index2],
        sale:that.data.sale,
      }
    }).then(res => {
      wx.hideLoading();
      wx.showToast({
        title: "修改成功",
      })
      setTimeout(function () {
        that.return()
      }, 1000) 
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: "修改失败",
      })
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    db.collection('Goods').doc(options.id).get().then(res => {
      // res.data 是一个包含集合中有权限访问的所有记录的数据，不超过 20 条
      console.log(res.data)
      let index=0;
      let index2=0;
      for(let i=0;i<this.data.array.length;i++){
        if(res.data.type==this.data.array[i]){
          index=i
        }
      }
      for(let i=0;i<this.data.express.length;i++){
        if(res.data.express==this.data.express[i]){
          index2=i
        }
      }
      this.setData({
        rmb:res.data.rmb,
        index:index,
        index2:index2,
        imgbox:res.data.fileIDs,
        imgbox2:res.data.fileIDs2,
        size:res.data.size,
        color:res.data.color,
        title:res.data.title,
        address:res.data.address,
        sale:res.data.sale,
        numbers:res.data.numbers,
        id:res.data._id
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})